//
//  QWeiboAsyncApi.m
//  QWeiboSDK4iOSDemo
//
//  Created   on 11-1-18.
//   
//

#import "QWeiboAsyncApi.h"
#import "QOauthKey.h"
#import "QweiboRequest.h"

@interface NSMutableDictionary(addx) {
}
- (void)setLongLong:(long long)idx forKey:(id)aKey;
- (void)setLong:(long)idx forKey:(id)aKey;
@end
@implementation NSMutableDictionary(addx)
- (void)setLongLong:(long long)idx forKey:(id)aKey {
    [self setObject:[NSString stringWithFormat:@"%lld", idx] forKey:aKey];
}
- (void)setLong:(long)idx forKey:(id)aKey {
    [self setObject:[NSString stringWithFormat:@"%ld", idx] forKey:aKey];
}
@end



@implementation QWeiboAsyncApi


-(NSURLConnection*) getWithConsumerKey:(NSString *)aConsumerKey
                        consumerSecret:(NSString *)aConsumerSecret 
                        accessTokenKey:(NSString *)aAccessTokenKey 
                     accessTokenSecret:(NSString *)aAccessTokenSecret 
                            resultType:(ResultType)aResultType 
                              delegate:(id)aDelegate

                                getUrl:(NSString *)url
                            parameters:(NSMutableDictionary *)parameters
{
    QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
    
    NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
    if (!parameters) {
        parameters = [NSMutableDictionary dictionary];
    }
	[parameters setObject:format forKey:@"format"];
    
    QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url 
                                                    httpMethod:@"GET" 
                                                      oauthKey:oauthKey 
                                                    parameters:parameters 
                                                         files:nil 
                                                      delegate:aDelegate];
	[request release];
    
	[oauthKey release];
	return connection;
}

-(NSURLConnection*)postWithConsumerKey:(NSString *)aConsumerKey
                        consumerSecret:(NSString *)aConsumerSecret 
                        accessTokenKey:(NSString *)aAccessTokenKey 
                     accessTokenSecret:(NSString *)aAccessTokenSecret 
                            resultType:(ResultType)aResultType 
                              delegate:(id)aDelegate

                                getUrl:(NSString *)url
                            parameters:(NSMutableDictionary *)parameters
{
    QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
    
    NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
    if (!parameters) {
        parameters = [NSMutableDictionary dictionary];
    }
	[parameters setObject:format forKey:@"format"];
    
    QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url 
                                                    httpMethod:@"POST" 
                                                      oauthKey:oauthKey 
                                                    parameters:parameters 
                                                         files:nil 
                                                      delegate:aDelegate];
	[request release];
    
	[oauthKey release];
	return connection;
}








- (NSURLConnection*)getFriendshipWithConsumerKey:(NSString *)aConsumerKey
                                  consumerSecret:(NSString *)aConsumerSecret 
                                  accessTokenKey:(NSString *)aAccessTokenKey 
                               accessTokenSecret:(NSString *)aAccessTokenSecret 
                                      resultType:(ResultType)aResultType 
                                            name:(NSString *)name
                                        delegate:(id)aDelegate
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:name forKey:@"names"];
    [parameters setObject:@"2" forKey:@"flag"];
    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate
                             getUrl:@"http://open.t.qq.com/api/friends/check" 
                         parameters:parameters];
}

- (NSURLConnection*)postFollowWithConsumerKey:(NSString *)aConsumerKey
                               consumerSecret:(NSString *)aConsumerSecret 
                               accessTokenKey:(NSString *)aAccessTokenKey 
                            accessTokenSecret:(NSString *)aAccessTokenSecret 
                                   resultType:(ResultType)aResultType 
                                         name:(NSString *)name
                                     delegate:(id)aDelegate
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:name forKey:@"name"];
    
    return [self postWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                      accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                          resultType:aResultType delegate:aDelegate
                             getUrl:@"http://open.t.qq.com/api/friends/add" 
                          parameters:parameters];
}

- (NSURLConnection*)getUserInfoWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                      delegate:(id)aDelegate
{    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate
                             getUrl:@"http://open.t.qq.com/api/user/info"
                         parameters:nil];
}


//http://wiki.open.t.qq.com/index.php/%E5%B8%90%E6%88%B7%E7%9B%B8%E5%85%B3/%E4%BA%92%E5%90%AC%E5%85%B3%E7%B3%BB%E9%93%BE%E5%88%97%E8%A1%A8
- (NSURLConnection*) getFriendsWithConsumerKey:(NSString *)aConsumerKey
                               consumerSecret:(NSString *)aConsumerSecret 
                               accessTokenKey:(NSString *)aAccessTokenKey 
                            accessTokenSecret:(NSString *)aAccessTokenSecret 
                                   resultType:(ResultType)aResultType 
                                         name:(NSString *)name
                                   startindex:(int)startindex
                                       reqnum:(int)reqnum
                                      install:(int)install
                                     delegate:(id)aDelegate {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:name forKey:@"name"];
    [parameters setLong:startindex forKey:@"startindex"];
    [parameters setLong:reqnum forKey:@"reqnum"];
    [parameters setLong:install forKey:@"install"];
    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate
                             getUrl:@"http://open.t.qq.com/api/friends/mutual_list"
                         parameters:parameters];
}



- (NSURLConnection*) getInviteWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                      delegate:(id)aDelegate
{
    //http://wiki.open.t.qq.com/index.php/%E5%B8%90%E6%88%B7%E7%9B%B8%E5%85%B3/%E8%8E%B7%E5%8F%96%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8
    //appid
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:aConsumerKey forKey:@"appid"];
    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate
                             getUrl:@"http://open.t.qq.com/api/invite/get_invite_list"
                         parameters:parameters];
}

- (NSURLConnection*)getCommentsWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                          flag:(int)flag
                                        rootid:(long long)rootid
                                      pageflag:(int)pageflag
                                      pagetime:(long long)pagetime
                                        reqnum:(int)reqnum
                                     twitterid:(long long)twitterid
                                      delegate:(id)aDelegate
{
    //微博相关/获取单条微博的转发或点评列表
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setLong:flag forKey:@"flag"];
    [parameters setLongLong:rootid forKey:@"rootid"];
    [parameters setLong:pageflag forKey:@"pageflag"];
    [parameters setLong:pagetime forKey:@"pagetime"];
    [parameters setLong:reqnum forKey:@"reqnum"];
    [parameters setLong:twitterid forKey:@"twitterid"];
    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate 
                             getUrl:@"http://open.t.qq.com/api/t/re_list" 
                         parameters:parameters];
}

- (NSURLConnection*)   getWeiboWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                           idx:(long long)idx
                                      delegate:(id)aDelegate
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setLongLong:idx forKey:@"id"];
    NSString* url = @"http://open.t.qq.com/api/t/show";
    
    return [self getWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                     accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret 
                         resultType:aResultType delegate:aDelegate 
                             getUrl:url parameters:parameters];
}





- (NSURLConnection*)postCommentWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                          reid:(long long)reid
                                       content:(NSString*)content
                                      delegate:(id)aDelegate
{
    //微博相关/点评一条微博
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"127.0.0.1" forKey:@"clientip"];
    [parameters setObject:content forKey:@"content"];
    [parameters setLongLong:reid forKey:@"reid"];
    
    return [self postWithConsumerKey:aConsumerKey consumerSecret:aConsumerSecret 
                      accessTokenKey:aAccessTokenKey accessTokenSecret:aAccessTokenSecret
                          resultType:aResultType delegate:aDelegate 
                              getUrl:@"http://open.t.qq.com/api/t/comment" 
                          parameters:parameters];
}







//http://wiki.open.t.qq.com/index.php/%E6%90%9C%E7%B4%A2%E7%9B%B8%E5%85%B3/%E6%90%9C%E7%B4%A2%E5%BE%AE%E5%8D%9A
- (NSURLConnection*)searchWeiboWithConsumerKey:(NSString *)aConsumerKey
                                consumerSecret:(NSString *)aConsumerSecret 
                                accessTokenKey:(NSString *)aAccessTokenKey 
                             accessTokenSecret:(NSString *)aAccessTokenSecret 
                                    resultType:(ResultType)aResultType 
                                       keyword:(NSString*)keyword
                                      delegate:(id)aDelegate
{
    NSString *url = @"http://open.t.qq.com/api/search/t";
    QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
    
    NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:format forKey:@"format"];
    [parameters setObject:keyword forKey:@"keyword"];
    [parameters setObject:@"20" forKey:@"pagesize"];
    [parameters setObject:@"1" forKey:@"page"];
    
    QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil delegate:aDelegate];
	
	[request release];
	[oauthKey release];
	return connection;
}



- (NSURLConnection *)getHomeMsgWithConsumerKey:(NSString *)aConsumerKey
						 consumerSecret:(NSString *)aConsumerSecret 
						 accessTokenKey:(NSString *)aAccessTokenKey 
					  accessTokenSecret:(NSString *)aAccessTokenSecret 
							 resultType:(ResultType)aResultType 
							  pageFlage:(PageFlag)aPageFlag 
								nReqNum:(NSInteger)aReqNum 
							   delegate:(id)aDelegate {
	
	NSString *url = @"http://open.t.qq.com/api/statuses/home_timeline";
	
	QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
	
	NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
	
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:format forKey:@"format"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aPageFlag] forKey:@"pageflag"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aReqNum] forKey:@"reqnum"];
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil delegate:aDelegate];
	
	[request release];
	[oauthKey release];
	return connection;
}

- (NSURLConnection *)publishMsgWithConsumerKey:(NSString *)aConsumerKey 
						 consumerSecret:(NSString *)aConsumerSecret 
						 accessTokenKey:(NSString *)aAccessTokenKey 
					  accessTokenSecret:(NSString *)aAccessTokenSecret 
								content:(NSString *)aContent 
							  imageFile:(NSString *)aImageFile 
							 resultType:(ResultType)aResultType 
							   delegate:(id)aDelegate {
	
	NSMutableDictionary *files = [NSMutableDictionary dictionary];
	NSString *url;
	
	if (aImageFile) {
		url = @"http://open.t.qq.com/api/t/add_pic";
		[files setObject:aImageFile forKey:@"pic"];
	} else {
		url = @"http://open.t.qq.com/api/t/add";
	}
	
	QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
	/*
	NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}*/
	
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:aContent forKey:@"content"];
	[parameters setObject:@"127.0.0.1" forKey:@"clientip"];
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url httpMethod:@"POST" oauthKey:oauthKey parameters:parameters files:files delegate:aDelegate];
	
	[request release];
	[oauthKey release];
	return connection;
}

@end
